using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Security.Cryptography;

using Actions.Databases;

namespace Actions.Security
{
	/// <summary>
	/// User - class for system access personalization
	/// </summary>
	public class User
	{
		private	string		_login;
		private string		_name;
		private string		_mail;
		private string		_mailhash;
		private bool		_isValid;

		private Usecase		_defaultUsecase;
		private ArrayList	_roles;
				
		public string 		name
		{
			get
			{
				return _name;	
			}
		}		
		public string 		mail
		{
			get
			{
				return _mail;	
			}
		}		
		public string 		login
		{
			get
			{
				return _login;	
			}
		}				
		public bool			isValid
		{
			get
			{
				return _isValid;
			}
		}
		public Usecase 		defaultUsecase
		{
			get
			{
				return _defaultUsecase;	
			}
			set 
			{
				_defaultUsecase = value;
			}
		}		
		public ArrayList	roles
		{
			get
			{
				return 	_roles;
			}
		}
		
		public string 		userInfo
		{
			get
			{		
				string s = "<userinfo><login>" + _login + 
					"</login><name>" + _name + "</name><mailhash>" +
					_mailhash + "</mailhash><roles>";				
	
				foreach(Role r in _roles) {
					s += "<item>" + r.title + "</item>";
				}				
				s += "</roles></userinfo>";
				
				return s;
			}
		}

		public User()
		{
			//
			// Create anonymous user
			//
			_login = "";
			_name = "Anonymous";
			_isValid = true;			
			_defaultUsecase = new Usecase("Start");
			_roles = new ArrayList();
			_roles.Add(new Role());
		}
						
		public User(string dbid, string username, string password) 
		{
			QueryResult qr = Manager.i.db(dbid).
				query(string.Format("SELECT login FROM _users WHERE login='{0}' AND secret=MD5('{1}') AND enable", username, password));
			
			if(qr.res.Count == 1) {
				init(dbid, username);
			} else {
				_login = "";
				_name = "Anonymous";
				_isValid = true;			
				_defaultUsecase = new Usecase("Start");		
				_roles = new ArrayList();
				_roles.Add(new Role());
			}
		}

		public void init(string dbid, string username) 
		{
			QueryResult qr = Manager.i.db(dbid).
				query(string.Format("SELECT * FROM _users WHERE login='{0}' AND enable", username));
			
			if(qr.res.Count == 1) {
				_login = qr.got(0, "login");
				_name = qr.got(0, "name");
				_mail = qr.got(0, "mail");
				
				MD5 md5i = MD5.Create();
				byte[] dt = md5i.ComputeHash(Encoding.Default.GetBytes(_mail));
				StringBuilder sb = new StringBuilder();
		        for (int i = 0; i < dt.Length; i++) 
            		sb.Append(dt[i].ToString("x2"));        			
		        _mailhash = sb.ToString();
				
				_isValid = true;	
				_defaultUsecase = new Usecase(qr.got(0, "dusecase"));
				
				_roles = new ArrayList();				
				string[] r = qr.got(0, "roles").Split(',');
        		foreach (string ri in r) {
         			_roles.Add(new Role(dbid, ri));
				}											
			} 					
		}
		
		public bool checkUsecase(Usecase usecase)
		{
			bool result = false;
			
			foreach(Role r in _roles) {
				if(r.checkUsecase(usecase))  {
					result = true;
					break;
				}				
			}				
			return result;	
		}
		
	}
}
